package com.yan.demo03;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author yanheng
 * @data 2020/1/19 10:47
 */
public class JDKProxyFactory implements InvocationHandler {

    private Object targetObject;

    public Object createProxyInstance(Object targetObject){
        this.targetObject = targetObject;
//        System.getProperties().put("sun.misc.ProxyGenerator.saveGeneratedFiles", "true");
        return Proxy.newProxyInstance(targetObject.getClass().getClassLoader(),
                targetObject.getClass().getInterfaces(),
                this);
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        Object invoke = method.invoke(targetObject, args);
        return invoke;
    }
}
